package cn.topcodes.tcsf.admin.commons.api.convertor;

import cn.topcode.unicorn.utils.I18nUtil;
import cn.topcodes.tcsf.admin.commons.convertor.Entity2VoConvertor;
import cn.topcodes.tcsf.admin.commons.domain.Page;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Unicorn on 2017/10/1.
 */
public class HttpEntity2VoConvertor<T, V> extends Entity2VoConvertor<T, V> {

    public Page<V> convertPage(Page<T> pageEntity, HttpServletRequest request) {
        Assert.notNull(pageEntity);
        List<V> rows = convertList(pageEntity.getRows(),request);
        return new Page<V>(rows,pageEntity.getTotal(),pageEntity.getPageNumber(),pageEntity.getPageSize());
    }

    public List<V> convertList(List<T> entitys, HttpServletRequest request) {
        Assert.notNull(entitys);
        List<V> vos = new ArrayList<V>();
        for(T entity : entitys) {
            vos.add(convert(entity,request));
        }
        return vos;
    }

    public V convert(T entity, HttpServletRequest request) {
        V vo = this.convert(entity);
        I18nUtil.processAnnotation(request,vo);
        return vo;
    }
}
